package org.jeecg.modules.demo.mall.yxuserlevel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.mall.entity.YxUser;
import org.jeecg.modules.demo.mall.entity.YxUserLevel;
import org.jeecg.modules.demo.mall.yxuser.service.IYxUserService;
import org.jeecg.modules.demo.mall.yxuserlevel.service.IYxUserLevelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 用户等级记录表
 * @Author: jeecg-boot
 * @Date: 2023-10-26
 * @Version: V1.0
 */
@Api(tags = "yx_user_level")
@RestController
@RequestMapping("/mall/yxuserlevel/yxUserLevel")
@Slf4j
public class YxUserLevelController extends JeecgController<YxUserLevel, IYxUserLevelService> {
    @Autowired
    private IYxUserLevelService yxUserLevelService;

    @Autowired
    private IYxUserService yxUserService;

    /**
     * 分页列表查询
     *
     * @param yxUserLevel
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "yx_user_level-分页列表查询")
    @ApiOperation(value = "yx_user_level-分页列表查询", notes = "yx_user_level-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<YxUserLevel>> queryPageList(YxUserLevel yxUserLevel,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<YxUserLevel> queryWrapper = QueryGenerator.initQueryWrapper(yxUserLevel, req.getParameterMap());
        Page<YxUserLevel> page = new Page<YxUserLevel>(pageNo, pageSize);
        IPage<YxUserLevel> pageList = yxUserLevelService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param yxUserLevel
     * @return
     */
    @AutoLog(value = "yx_user_level-添加")
    @ApiOperation(value = "yx_user_level-添加", notes = "yx_user_level-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody YxUserLevel yxUserLevel) {
        yxUserLevelService.save(yxUserLevel);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param yxUserLevel
     * @return
     */
    @AutoLog(value = "yx_user_level-编辑")
    @ApiOperation(value = "yx_user_level-编辑", notes = "yx_user_level-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody YxUserLevel yxUserLevel) {
        yxUserLevelService.updateById(yxUserLevel);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "yx_user_level-通过id删除")
    @ApiOperation(value = "yx_user_level-通过id删除", notes = "yx_user_level-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) Integer id) {
        yxUserLevelService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "yx_user_level-批量删除")
    @ApiOperation(value = "yx_user_level-批量删除", notes = "yx_user_level-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.yxUserLevelService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "yx_user_level-通过id查询")
    @ApiOperation(value = "yx_user_level-通过id查询", notes = "yx_user_level-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<YxUserLevel> queryById(@RequestParam(name = "id", required = true) Integer id) {
        YxUserLevel yxUserLevel = yxUserLevelService.getById(id);
        if (yxUserLevel == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(yxUserLevel);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param yxUserLevel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxUserLevel yxUserLevel) {
        return super.exportXls(request, yxUserLevel, YxUserLevel.class, "yx_user_level");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxUserLevel.class);
    }


    /**
     * 充值扣除
     *
     * @param uid
     */
    @GetMapping(value = "/subgrowthValues")
    public void subgrowthValues(@RequestParam Integer uid, @RequestParam String orderId) {
        YxUser user = yxUserService.getById(uid);

        LambdaQueryWrapper<YxUserLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YxUserLevel::getOrderId, orderId);
        List<YxUserLevel> yxUserLevels = yxUserLevelService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(yxUserLevels) && yxUserLevels.size() == 1) {
            user.setUserGrowthValue(user.getUserGrowthValue().subtract(yxUserLevels.get(0).getGrowthValue()));
            YxUserLevel yxUserLevel = new YxUserLevel();
            BeanUtils.copyProperties(yxUserLevels.get(0), yxUserLevel);
            yxUserLevel.setId(null);
            yxUserLevel.setGrowthValue(yxUserLevels.get(0).getGrowthValue().negate());
            yxUserLevelService.save(yxUserLevel);
            yxUserService.updateById(user);
        }


    }

}
